<?php
/**
 * 会员中心
 * User: chen guang
 * Date: 2017/9/29 9:00
 *
 */

namespace app\admin\controller;
use think\Db;

class Member extends Common{
	
	/*
	*	构造函数
	*/
	function __construct(){
		parent::__construct();

	}

    /**
     * 会员列表
     */
    public function lists(){
		$where['delete']=1;
		$status=input("status");
		if($status!=''){
			$where['status']=$status;
		}
		$type=input("type");
		if($type!=''){
			$where['type']=$type;
		}
		if(input("keyword")){
			$where['nickname|mobile']=array("like",'%'.input('keyword').'%');
		}
        $list = db("member")->where($where)->order("id desc")->paginate(6);
        $this->assign('list',$list);
		$this->assign('status',input("status"));
		$this->assign('type',input("type"));
		$this->assign('keyword',input("keyword"));
        return $this->fetch();
    }

    /**
     * 导出
     */
    public function exportExcel(){
        $where['delete']=1;
        if(input("keyword")){
            $where['nickname|mobile'] = array("like",'%'.input('keyword').'%');
        }
        $list = db("member")->field('id,nickname,mobile,headimage,register_time')->where($where)->order("id desc")->select();
        $PHPExcel = new \PHPExcel();
        $PHPSheet = $PHPExcel->getActiveSheet();
        $name = TIMESTAMP.rand(1000,9999);
        $PHPSheet->setTitle($name);
        $PHPSheet->setCellValue('A1','会员编号')
            ->setCellValue('B1','昵称')
            ->setCellValue('C1','手机号')
            ->setCellValue('D1','头像地址')
            ->setCellValue('E1','创建时间');
        foreach($list as $n => $l){
            $PHPSheet->setCellValue('A'.($n+2),$l['id'])
                ->setCellValue('B'.($n+2),$l['nickname'])
                ->setCellValue('C'.($n+2),$l['mobile'])
                ->setCellValue('D'.($n+2),$l['headimage'])
                ->setCellValue('E'.($n+2),date('Y-m-d H:i:s',$l['register_time']));
        }
        $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007');
        $PHPWriter->save('report/'.$name.'.xlsx');
        $this->redirect('./report/'.$name.'.xlsx');
    }


	/*
  * 设置黑名单
  */
	public function setblack(){
		$id = input('id/d');
		if(!in_array(input('status'),array(0,1))){
			$this->error("状态有误");
		}
		$data['status'] = input('status');
		$res=db("member")->where("id='$id'")->update($data);
		if($res) $this->redirect($_SERVER['HTTP_REFERER']);
		$this->error("操作失败");
	}
	
}